<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card" :body-style="{ padding: '20px' }">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" style="padding: 20px">
            <el-row style="height:60px">
              <el-button type="primary" size="small" @click="addRole">+新增角色</el-button>
            </el-row>
            <el-table :data="tableData" border="">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click.native.prevent="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" align="middle" style="height: 60px">
              <span class="demonstration">共{{ page.total }}条</span>
              <!-- 分页 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                layout="prev, pager, next"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-row type="flex" justify="center" style="padding:20px">
              <el-form label-width="120px" style="margin-top:50px">
                <el-form-item label="企业名称">
                  <el-input
                    v-model="formData.name"
                    placeholder="请输入内容"
                    :disabled="true"
                    style="width:400px"
                  />

                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input
                    v-model="formData.companyAddress"
                    placeholder="请输入内容"
                    :disabled="true"
                    style="width:400px"
                  />
                </el-form-item>
                <el-form-item label="公司电话">
                  <el-input
                    v-model="formData.companyPhone"
                    placeholder="请输入内容"
                    :disabled="true"
                    style="width:400px"
                  />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input
                    v-model="formData.mailbox"
                    placeholder="请输入内容"
                    :disabled="true"
                    style="width:400px"
                  />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input
                    v-model="formData.remarks"
                    placeholder="请输入内容"
                    :disabled="true"
                    style="width:400px"
                    type="textarea"
                    :rows="3"
                  />
                </el-form-item>
              </el-form>
            </el-row>

          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog title="编辑角色" :visible="dialogTableVisible" @close="cancel">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="btnOK">确定</el-button>
        </el-row>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog :visible.sync="isShow" title="分配权限">
        <el-tree
          ref="tree"
          :data="permissions"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          :check-strictly="true"
          :props="defaultProps"
        />
        <el-row type="flex" justify="center" @close="clear">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnAssignPerm">确定</el-button>
            <el-button size="small" @click="clear">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { getPermissionList, assignPerm } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      activeName: 'first',
      tableData: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      },
      formData: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      dialogTableVisible: false,
      form: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      permissions: [],
      isShow: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      userId: null,
      selectCheck: []
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      // console.log(total)
      this.tableData = rows
      // console.log(this.tableData)
      this.page.total = total
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    async getCompanyInfo() {
      // console.log(this.$store.getters.companyId)
      this.formData = await getCompanyInfo(this.$store.getters.companyId)
    },
    async deleteRole(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    async editRole(id) {
      this.dialogTableVisible = true
      this.form = await getRoleDetail(id)
    },
    async btnOK() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          await updateRole(this.form)
        } else {
          await addRole(this.form)
        }
        this.getRoleList()
        this.dialogTableVisible = false
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.form = {}
      this.$refs.form.resetFields()
      this.dialogTableVisible = false
    },
    addRole() {
      this.dialogTableVisible = true
    },
    async assignPerm(id) {
      this.isShow = true
      const result = await getPermissionList()
      this.permissions = tranListToTreeData(result, '0')
      // console.log(this.permissions)
      this.userId = id
      const { permIds } = await getRoleDetail(this.userId)
      this.selectCheck = permIds
      console.log(permIds)
    },
    async btnAssignPerm() {
      // console.log(this.selectCheck)
      const result = this.$refs.tree.getCheckedKeys(true)
      await assignPerm({ permIds: [...result], id: this.userId })
      this.$message.success('修改权限成功')
      this.isShow = false
    },
    clear() {
      this.selectCheck = []
      this.isShow = false
    }
  }
}
</script>

<style>
</style>
